package com.nbgroup.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.nbgroup.mapper.ClueMapper;
import com.nbgroup.mapper.IndexMapper;
import com.nbgroup.pojo.Clue;
import com.nbgroup.pojo.DeptMent;
import com.nbgroup.pojo.common.Result;
import com.nbgroup.pojo.vo.BusinessListByOwnerVO;
import com.nbgroup.pojo.vo.ClueListByOwnerVO;
//import com.nbgroup.pojo.vo.SalesVO;
import com.nbgroup.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl  implements IndexService {

    @Autowired
    private ClueMapper clueMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Set getSalesStatistic(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<ClueListByOwnerVO> vos = clueMapper.listByOwner(beginCreateTime,endCreateTime);
        if (CollectionUtils.isEmpty(vos)){
            return null;
        }
        Double r = 0d;
        for (ClueListByOwnerVO vo : vos) {
            r = (double) (vo.getNum()/vo.getCount());
            vo.setRadio(r);
            vo.setCreate_by(vo.getCreateBy());
        }
        vos.forEach(vo -> {
            redisTemplate.opsForZSet().add("ClueRate",vo,vo.getRadio());
        });
        Set ranged = redisTemplate.opsForZSet().range("ClueRate", 0, -1);
        redisTemplate.opsForZSet().removeRange("ClueRate",0,-1);
        return ranged;
    }

    @Override
    public Result getBusinessStatistic(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<ClueListByOwnerVO> list = new ArrayList<>();
        ClueListByOwnerVO vo1 = ClueListByOwnerVO.builder().num(10).radio(84.6).create_by("张三").deptName("销售部").build();
        ClueListByOwnerVO vo = ClueListByOwnerVO.builder().num(6).radio(66.6).create_by("李四").deptName("销售部").build();
        list.add(vo);
        list.add(vo1);
        List<BusinessListByOwnerVO> vos = BeanUtil.copyToList(list, BusinessListByOwnerVO.class);
        return Result.success(vos);
    }
}
